import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


public class SystemTimer extends IODevice
{	
	private static final int TIMER_DELAY = 15000;
	private CPU my_cpu;
	private Timer my_timer;
	
	public SystemTimer(final int the_id, final String the_name)
	{
		super (the_id, the_name);
		setTimer();
	}
	
	public void setTimer()
	{
		my_timer = new Timer(TIMER_DELAY, new TimerListener());
		my_timer.setInitialDelay(TIMER_DELAY);
	}
	
	public void setCPU()
	{
		my_cpu = super.getCPU();
	}
	
	public void stop()
	{
		my_timer.stop();
	}
	
	public Timer getTimer()
	{
		return my_timer;
	}
	
	public void run()
	{
		my_timer.start();
		setCPU();
	}
	
	private void sendInterrupt()
	{
		super.getCPU().handleInterrupt(this);
	}
	
	private class TimerListener implements ActionListener
	{
		public void actionPerformed(final ActionEvent the_event)
		{
			sendInterrupt();
			my_cpu.switchProcess();
		}
	}
}
